package com.itheima.student.system07.caozuostudent;


import com.itheima.student.system07.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.ServiceConfigurationError;


/*
操作学生信息的类

属性
键盘录入   集合

行为
增
删
改
查


 */
public class CaoZuoStudent {

    private Scanner sc = new Scanner(System.in);
    private ArrayList<Student> list = new ArrayList<>();
    // 添加学生
    public void addCaoZuoStudent() {
        System.out.println("请输入要录入学生的学号:");
        String sid = null;
        while (true) {
            sid = sc.next();
            int index = getIndex(sid);
            if (index < 0) {
                System.out.println("请继续下一环节:");
                Student student = getStudent(sid);
                list.add(student);
                System.out.println("学生信息已添加!");
                break;
            } else {
                System.out.println("系统中已有此学生,请查看正确的学号再录入!!!");

            }
        }
    }
    //   删除学生
    public void removeCaoZuoStudent() {
        if (list.size() == 0) {
            System.out.println("系统中没有学生,请先添加学生!");
        }
        System.out.println("请输入要录入学生的学号:");
        String sid = null;
        while (true) {
            sid = sc.next();
            int index = getIndex(sid);
            if (index >= 0) {
                list.remove(index);
                System.out.println("删除成功!");
                break;
            } else {
                System.out.println("系统中没有此学号,不必删除!");
                break;
            }
        }

    }
    // 修改学生信息
    public void updateCaoZuoStudent() {
        System.out.println("请输入要修改学生的学号:");
        String sid = null;
        while (true) {
            sid = sc.next();
            int index = getIndex(sid);
            if (index >= 0) {
                System.out.println("请继续下一环节:");
                Student student = getStudent(sid);
                list.set(index, student);
                System.out.println("学生信息已更新!");
                break;
            } else {
                System.out.println("系统中没有此学生,请先录入!");
                return;
            }
        }
    }
    // 查看学生信息
    public void findCaoZuoStudent() {
        if (list.size() == 0) {
            System.out.println("系统中没有学生,请先添加学生信息!");
        }else{
            System.out.println("请输入你的选择:");
            System.out.println("1.查看指定学生信息:");
            System.out.println("2.查看所有学生信息:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    IndividualStudents();
                    break;
                case "2":
                    findAllStudent();
                    return;
                default:
                    System.out.println();
                    break;
            }
        }
    }

    //  查看单独的学生
    public void IndividualStudents() {
        System.out.println("请输入你要查看的学生的学号:");
        while (true) {
            String sid = sc.next();
            int index = getIndex(sid);
            if (index >= 0) {
                Student student = list.get(index);
                System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
                System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getBirthday());
                break;
            } else {
                System.out.println("系统中没有此学生,请输入正确的学号!");
            }
        }
    }

    public void findAllStudent() {
        System.out.println("学号" + "\t" + "姓名" + "\t" + "年龄" + "\t" + "性别" + "\t" + "生日");
        for (int i = 0; i < list.size(); i++) {
            // list.get(i)
            Student student = list.get(i);

            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getBirthday());

        }
    }

    //  判断系统中是否存在此学生
    private int getIndex(String sid) {
        for (int i = 0; i < list.size(); i++) {
            // list.get(i)
            Student student = list.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

    //
    public Student getStudent(String sid) {
        System.out.println("请输入要录入学生的姓名:");
        String name = sc.next();
        System.out.println("请输入要录入学生的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入要录入学生的生日:");
        String birthday = sc.next();
        System.out.println("请输入要录入学生的性别:");
        String gender = sc.next();
        return new Student(sid, name, age, birthday, gender);
    }
}
